<template>
	<view class="good-item">
	  <!-- 左侧区域 -->
	  <view class="left-item">
      <radio :checked="goods.goods_state" color="#C00000" v-if="showRadio" @click="radioClickHandler"></radio>
	    <image :src="goods.goods_small_logo || defaultPic" mode="scaleToFill"></image>
	  </view>
	  <!-- 右侧区域 -->
	  <view class="right-item">
	    <view class="goods-name">{{goods.goods_name}}</view>
	   <view class="goods-info-box">
	     <view class="goods-price">￥{{goods.goods_price | toFixed}}</view>
	     <uni-number-box :min="1" :value="goods.goods_count" @change="numberClickHandler" v-if="showNum"></uni-number-box>
	   </view>
	  </view>
	</view>
</template>

<script>
	export default {
      // 定义 props 属性，用来接收外界传递到当前组件的数据
        props: {
          // 商品的信息对象
          goods: {
            type: Object,
            default: {},
          },
          // 是否展示图片左侧的 radio
          showRadio:{
            type: Boolean,
            // 如果外界没有指定 show-radio 属性的值，则默认不展示 radio 组件
            default: false
          },
           // 是否展示价格右侧的 NumberBox 组件
           showNum:{
             type:Boolean,
             default: false
           }
        },
    
		data() {
			return {
				// 默认的空图片
				defaultPic:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fqqpublic.qpic.cn%2Fqq_public%2F0%2F0-2887165668-BDAFB8705B0776D851B17F12C7C64818%2F0%3Ffmt%3Djpg%26size%3D219%26h%3D800%26w%3D523%26ppv%3D1.jpg&refer=http%3A%2F%2Fqqpublic.qpic.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639111348&t=081a2b606df8a4373212c76e2e57e60a'
			};
		},
    // 过滤器处理商品价格
    filters:{
      toFixed(num) {
       return Number(num).toFixed(2)
      }
    },
    methods:{
      // 点击radio 改变勾选状态
      radioClickHandler(){
        // 通过 this.$emit() 触发外界通过 @ 绑定的 radio-change 事件，
            // 同时把商品的 Id 和 勾选状态 作为参数传递给 radio-change 事件处理函数
        this.$emit('radio-change',{
          goods_id: this.goods.goods_id,
          // 商品最新的勾选状态
          goods_state: !this.goods.goods_state
        })
      },
      
      // 点击numberClickHandler 触发数量变化事件
      numberClickHandler(value){
        console.log(value)
        this.$emit('num-change',{
          goods_id: this.goods.goods_id,
          goods_count: +value
        })
      }
    }
	}
</script>

<style lang="scss">
.good-item{
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;
    
    .left-item{
      margin-right: 5px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      image{
        width: 100px;
        height: 100px;
        
        display: block;
      }
      
      
    }
    
    .right-item{
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      
      .goods-name {
        font-size: 13px;
      }
      
      .goods-price{
         font-size: 16px;
         color: #c00000;
      }
    }
  }
  
  .goods-info-box{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
